'use strict';

var deviceObj={
	video:true,
	audio:true
}

var offerOptions = {
  offerToReceiveAudio: 1,
  offerToReceiveVideo: 1
};

var server = null;
var pcConstraint = null;
var dataConstraint = null;

var sendChannel = null;
var receiveChannel = null;

function initialise(mediaObj){
	navigator.mediaDevices.getUserMedia(mediaObj).then(init_success,init_error).catch(function(e){
		console.log("获取媒体资源时出错");
		console.log(e);
	});
}

function init_success(stream){
	console.log(stream);
	vm.localVideo = window.URL.createObjectURL(stream);
	vm.localStream = stream;
	vm.startBtn = true;
	vm.callBtn = false;
}

function init_error(err){
	console.log("失败");
}

function addStream(){
	vm.remote = new RTCPeerConnection(server,pcConstraint);
	vm.local = new RTCPeerConnection(server,pcConstraint);
	
	console.log("创建 发送数据 通道");
	sendChannel = vm.local.createDataChannel("sendDataChannel",dataConstraint);
	sendChannel.onopen= onSendChannelStateChange;
	sendChannel.onclose = onSendChannelStateChange;
	
	vm.local.onicecandidate = function(e){
		onIceCandidate(vm.local,e,"local");
	}
	
	vm.local.oniceconnectionstatechange = function(e){
		onIceStateChange(vm.local, e,"local");
	}
	
	vm.remote.onicecandidate = function(e){
		onIceCandidate(vm.remoteremote,e,"remote");
	}
	
	vm.remote.oniceconnectionstatechange = function(e){
		    onIceStateChange(vm.local, e,"remote");
	}
	
	vm.remote.onaddstream = function(event){
		console.log(event.stream);
		vm.remoteVideo =  window.URL.createObjectURL(event.stream);
		vm.remoteStream = event.stream;
	}
	
	vm.remote.ondatachannel = receiveChannelCallback;
	
	vm.local.addStream(vm.localStream);
	console.log("添加本地音视频流");
	
	vm.local.createOffer(offerOptions).then(
		onCreateOfferSuccess,
		onCreateSessionDescriptionError);
}

function getOther(pc){
	return (pc === vm.local) ? vm.remote : vm.local;
}

function onIceCandidate(pc,event,name){
	if(event.candidate){
		getOther(pc).addIceCandidate(new RTCIceCandidate(event.candidate)).then(
			function(name){
				console.log(name+" 添加ICECandidate成功");
			},
			function(name){
				console.log(name+" 添加ICECandidate失败");
			}
		);
	}
}

function onIceStateChange(pc,event,name){
	console.log("ICE state change event: ",event);
}

function onCreateSessionDescriptionError(error) {
	console.log("创建offer失败： "+error.toString());
}

function onCreateOfferSuccess(desc){
	console.log("local 开始设置本地描述符");
	vm.local.setLocalDescription(desc).then(onSetLocalSuccess,onSetSessionDescriptionError);
	console.log("remote 开始设置远程描述符");
	vm.remote.setRemoteDescription(desc).then(onSetRemoteSuccess,onSetSessionDescriptionError);
	console.log("remote 开始创建应答");
	vm.remote.createAnswer().then(onCreateAnswerSuccess,onCreateSessionDescriptionError);
}

function onSetLocalSuccess(pc) {
  console.log("设置local description成功");
}

function onSetRemoteSuccess(pc) {
  console.log("设置remote description成功");
}

function onSetSessionDescriptionError(error) {
	console.log("设置失败"+error.toString());
}

function onCreateAnswerSuccess(desc){
	console.log("remote 设置本地描述符")
	vm.remote.setLocalDescription(desc).then(onSetLocalSuccess,onSetSessionDescriptionError);
	console.log("local 设置远程描述符");
	vm.local.setRemoteDescription(desc).then(onSetRemoteSuccess,onSetSessionDescriptionError);
}

function closeStream(){
	console.log("关闭呼叫");
	vm.local.close();
	vm.remote.close();
	vm.local = null;
	vm.remote = null;
}

function onSendChannelStateChange(){
	var readyState = sendChannel.readyState;
	console.log("Send Channel 状态： "+readyState);
	if(readyState === 'open'){
		console.log("传输通道就绪");
	}else{
		console.log("传输通道还没准备好");
	}
}

function receiveChannelCallback(event){
	receiveChannel = event.channel;
	receiveChannel.onmessage = onReceiveMessageCallback;
	receiveChannel.onopen = onReceiveChannelStateChange;
  	receiveChannel.onclose = onReceiveChannelStateChange;
}

function onReceiveMessageCallback(event) {
	console.log("接收到信息: "+event.data);
    vm.remoteText = event.data;
}

function onReceiveChannelStateChange(){
	var readyState = receiveChannel.readyState;
	console.log("接收通道发生改变： "+readyState);
}

function sendData(){
	var data = vm.localText;
	sendChannel.send(data);
	console.log("发送数据： "+data);
}
